package cn.bytecollege.admin.controller; /**
 * @Auther：MR.P
 * @Date:2021/10/28 19 29
 * @Description:修改支付方式，并跳转到支付方式列表页面
 */

import cn.bytecollege.admin.service.PaymentService;
import cn.bytecollege.common.entity.Payment;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "PaymentUpdateController", value = "/admin/paymentUpdate")
public class PaymentUpdateController extends HttpServlet {
    private PaymentService paymentService = null;
    public PaymentUpdateController(){
        paymentService = new PaymentService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String msg = null;
        String id = request.getParameter("paymentId");
        Integer paymentId = Integer.valueOf(id);
        String paymentName = request.getParameter("paymentName");
        //根据分类名字查询分类
        Payment payment = paymentService.findByPaymentName(paymentName);
        //将数据封装在tempPayment
        Payment tempPayment = new Payment();
        tempPayment.setPaymentId(paymentId);
        tempPayment.setPaymentName(paymentName);
        //payment不为空，那说明已存在相同名字
        if(payment != null){
            msg = "该支付方式名字已存在，请更换名字";
            request.setAttribute("msg",msg);
            request.setAttribute("payment",tempPayment);
            request.getRequestDispatcher("/admin/html/device/paymentUpdate.jsp").forward(request,response);
            return;
        }
        //进行修改
        paymentService.update(tempPayment);
        //重定向防止重复修改
        response.sendRedirect(request.getContextPath()+"/admin/paymentList");
    }
}
